package com.wanmait.wisdom.manage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wisdom.entity.House;
import com.wanmait.wisdom.entity.Owner;
import com.wanmait.wisdom.manage.service.ManageOwnerService;
import com.wanmait.wisdom.mapper.BuildingMapper;
import com.wanmait.wisdom.mapper.HouseMapper;
import com.wanmait.wisdom.mapper.OwnerMapper;

import com.wanmait.wisdom.mapper.UnitMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ManageOwnerServiceImpl  extends ServiceImpl<OwnerMapper, Owner> implements ManageOwnerService {
    @Resource
    private OwnerMapper ownerMapper;
    @Resource
    private UnitMapper unitMapper;
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private HouseMapper houseMapper;


    @Override
    public Page<Owner> selectAllOwnerInfo(Integer pageNum, Integer pageSize) {
        Page<Owner> page = new Page<>(pageNum,pageSize);
        Page<Owner> ownerPage = ownerMapper.selectPage(page,null);
        ownerPage.getRecords().forEach(owner -> {owner.setBuilding(buildingMapper.selectById(owner.getBuildingId()));});
        ownerPage.getRecords().forEach(owner -> {owner.setUnit(unitMapper.selectById(owner.getUnitId()));});
        ownerPage.getRecords().forEach(owner -> {owner.setHouse(houseMapper.selectById(owner.getHouseId()));});
        return ownerPage;
    }

    @Override
    public Owner selectOwnerById(Integer Id) {
        return ownerMapper.selectOwnerById(Id);
    }

    @Override
    public int updateOwner(Owner owner) {
        return ownerMapper.updateByPrimaryKey(owner);
    }

    @Override
    public Long getOwnerCount() {
        QueryWrapper<Owner>  queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("enable",1);
        return ownerMapper.selectCount(queryWrapper);
    }

    @Override
    public PageInfo<Owner> findOwnerByCondition(Owner owner, Integer pageNum, Date startTime, Date endTime, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(ownerMapper.findOwnerByCondition(owner,startTime,endTime));
    }
}
